package com.mt.roll.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.*;
import com.mt.roll.core.dal.service.EquipmentServiceBiz;
import com.mt.roll.core.dal.service.WorkSequenceEquipmentServiceBiz;
import com.mt.roll.core.dal.service.WorkSequenceServiceBiz;
import com.mt.roll.core.errorcode.ApiErrorCode;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.process.mapper.WorkSequenceVoMapper;
import com.mt.roll.process.params.WorkSequenceParam;
import com.mt.roll.process.params.WorkSequenceQueryParam;
import com.mt.roll.process.service.WorkSequenceService;
import com.mt.roll.process.vo.EquipmentVo;
import com.mt.roll.process.vo.WorkSequenceVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liguanghao
 * @Date: 2021/3/31 16:16
 * @Version 1.0
 */
@Service
@Transactional
public class WorkSequenceServiceImpl extends BaseService implements WorkSequenceService {

    @Resource
    private WorkSequenceVoMapper workSequenceVoMapper;
    @Resource
    private WorkSequenceServiceBiz workSequenceServiceBiz;
    @Resource
    private WorkSequenceEquipmentServiceBiz workSequenceEquipmentServiceBiz;
    @Resource
    private EquipmentServiceBiz equipmentServiceBiz;

    @Override
    public R<WorkSequenceVo> get(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER,idParam.getId());
        WorkSequence workSequence = workSequenceServiceBiz.getById(idParam.getId());
        WorkSequenceVo workSequenceVo=WorkSequenceVo.builder().build();
        BeanUtils.copyProperties(workSequence,workSequenceVo);
        QueryWrapper<WorkSequenceEquipment> wrapper=new QueryWrapper<>();
        wrapper.eq(WorkSequenceEquipment.WORK_SEQUENCE_ID,idParam.getId());
        List<WorkSequenceEquipment> workSequenceEquipments = workSequenceEquipmentServiceBiz.list(wrapper);
        List<EquipmentVo> equiupments=new ArrayList<>();
        if (workSequenceEquipments!=null){
            for (WorkSequenceEquipment workSequenceEquipment :workSequenceEquipments
                    ) {
                Equipment equipment = equipmentServiceBiz.getById(workSequenceEquipment.getEquipmentId());
                EquipmentVo equipmentVo=EquipmentVo.builder().build();
                BeanUtils.copyProperties(equipment,equipmentVo);
                equiupments.add(equipmentVo);
            }
        }
        workSequenceVo.setEquipments(equiupments);
        return successful(workSequenceVo);
    }

    @Override
    public R<PageVo<WorkSequenceVo>> page(WorkSequenceQueryParam workSequenceQueryParam) {
        QueryWrapper<WorkSequence> wrapper =new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(workSequenceQueryParam.getKey()),WorkSequence.NAME,workSequenceQueryParam.getKey());
        wrapper.orderByDesc(WorkSequence.CREATE_TIME);
        Page<WorkSequence> page = workSequenceServiceBiz.page(new Page<>(workSequenceQueryParam.getCurrent(), workSequenceQueryParam.getSize()), wrapper);
        return successful(new PageVo<>(page,WorkSequenceVo.class));
    }

    @Override
    public R<List<WorkSequenceVo>> list(WorkSequenceQueryParam workSequenceQueryParam) {
        QueryWrapper<WorkSequence> wrapper =new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(workSequenceQueryParam.getKey()),WorkSequence.NAME,workSequenceQueryParam.getKey());
        wrapper.orderByDesc(WorkSequence.CREATE_TIME);
        wrapper.eq(WorkSequence.VALID,1);
        List<WorkSequence> workSequenceList = workSequenceServiceBiz.list(wrapper);
        List<WorkSequenceVo> workSequenceVos = com.mt.roll.core.utils.BeanUtils.copyList(workSequenceList, WorkSequenceVo.class);
        for (WorkSequenceVo workSequenceVo:workSequenceVos
             ) {
            QueryWrapper<WorkSequenceEquipment> equipmentQueryWrapper=new QueryWrapper<>();
            equipmentQueryWrapper.eq(WorkSequenceEquipment.WORK_SEQUENCE_ID,workSequenceVo.getId());
            List<WorkSequenceEquipment> workSequenceEquipments = workSequenceEquipmentServiceBiz.list(equipmentQueryWrapper);
            List<EquipmentVo> equiupments=new ArrayList<>();
            if (workSequenceEquipments!=null){
                for (WorkSequenceEquipment workSequenceEquipment :workSequenceEquipments
                ) {
                    Equipment equipment = equipmentServiceBiz.getById(workSequenceEquipment.getEquipmentId());
                    EquipmentVo equipmentVo=EquipmentVo.builder().build();
                    BeanUtils.copyProperties(equipment,equipmentVo);
                    equiupments.add(equipmentVo);
                }
            }
            workSequenceVo.setEquipments(equiupments);
        }
        return successful(workSequenceVos);
    }

    @Override
    public R<IdVo> add(WorkSequenceParam workSequenceParam) {
        QueryWrapper<WorkSequence> wrapper=new QueryWrapper<>();
        wrapper.eq(ProcessFlow.NAME,workSequenceParam.getName());
        Assert.eqZero(workSequenceServiceBiz.count(wrapper),"工序名称已存在");
        WorkSequence workSequence=new WorkSequence();
        BeanUtils.copyProperties(workSequenceParam,workSequence);
        setCommonField(workSequence);
        workSequenceServiceBiz.save(workSequence);

        if (workSequenceParam.getEquipmentIds()!=null){
            for (Long id :workSequenceParam.getEquipmentIds()
                    ) {
                List<WorkSequenceEquipment> list = workSequenceEquipmentServiceBiz.list(new QueryWrapper<WorkSequenceEquipment>()
                        .eq(WorkSequenceEquipment.EQUIPMENT_ID, id).eq(WorkSequenceEquipment.VALID, 1));
                if (list.size()!=0){
                    workSequenceServiceBiz.removeById(workSequence);
                   return failed("该工序选择的设备已被其他工序绑定！不能重复绑定！请重新选择！") ;
                }
                WorkSequenceEquipment workSequenceEquipment=new WorkSequenceEquipment();
                workSequenceEquipment.setEquipmentId(id);
                workSequenceEquipment.setWorkSequenceId(workSequence.getId());
                setCommonField(workSequenceEquipment);
                workSequenceEquipmentServiceBiz.save(workSequenceEquipment);
            }
        }
        return successful(IdVo.builder().id(workSequence.getId()).build());
    }

    @Override
    public R<IdVo> update(WorkSequenceParam workSequenceParam) {
        WorkSequence workSequence = workSequenceServiceBiz.getById(workSequenceParam.getId());
        if (!workSequence.getName().equals(workSequenceParam.getName())){
            QueryWrapper<WorkSequence> wrapper=new QueryWrapper<>();
            wrapper.eq(WorkSequence.NAME,workSequenceParam.getName());
            Assert.eqZero(workSequenceServiceBiz.count(wrapper),"工序名称已存在");
        }
        WorkSequence updateWorkSequence=new WorkSequence();
        BeanUtils.copyProperties(workSequenceParam,updateWorkSequence);
        setUpdateCommonField(updateWorkSequence);
        workSequenceServiceBiz.updateById(updateWorkSequence);
        if (workSequenceParam.getEquipmentIds()!=null){
            QueryWrapper<WorkSequenceEquipment> workSequenceEquipmentQueryWrapper=new QueryWrapper<>();
            workSequenceEquipmentQueryWrapper.eq(WorkSequenceEquipment.WORK_SEQUENCE_ID,workSequence.getId());
            workSequenceEquipmentServiceBiz.remove(workSequenceEquipmentQueryWrapper);
            for (Long id:workSequenceParam.getEquipmentIds()
                 ) {
                WorkSequenceEquipment workSequenceEquipment=new WorkSequenceEquipment();
                workSequenceEquipment.setEquipmentId(id);
                workSequenceEquipment.setWorkSequenceId(workSequence.getId());
                setCommonField(workSequenceEquipment);
                workSequenceEquipmentServiceBiz.save(workSequenceEquipment);
            }
        }else {
            QueryWrapper<WorkSequenceEquipment> workSequenceEquipmentQueryWrapper=new QueryWrapper<>();
            workSequenceEquipmentQueryWrapper.eq(WorkSequenceEquipment.WORK_SEQUENCE_ID,workSequence.getId());
            workSequenceEquipmentServiceBiz.remove(workSequenceEquipmentQueryWrapper);
        }
        return successful(IdVo.builder().id(workSequence.getId()).build());
    }

    @Override
    public R<IdVo> delete(IdParam idParam) {
        WorkSequence workSequence = workSequenceServiceBiz.getById(idParam.getId());
        if (workSequence == null) {
            return failed("工序不存在");
        }
        workSequenceServiceBiz.removeById(idParam.getId());
        List<WorkSequenceEquipment> list = workSequenceEquipmentServiceBiz.list(new QueryWrapper<WorkSequenceEquipment>()
                .eq(WorkSequenceEquipment.WORK_SEQUENCE_ID, idParam.getId()).eq(WorkSequenceEquipment.VALID, 1));
        List<Long> idList=new ArrayList<>();
        for (WorkSequenceEquipment workSequenceEquipment:list
             ) {
            idList.add(workSequenceEquipment.getId());
        }
        workSequenceEquipmentServiceBiz.removeByIds(idList);
        return successful(IdVo.builder().id(workSequence.getId()).build());
    }

    @Override
    public R<List<EquipmentVo>> getEquipmentlist() {
        List<Equipment> equipmentList = equipmentServiceBiz.list(new QueryWrapper<Equipment>().eq(Equipment.EQUIPMENT_TYPE, 2));
        List<EquipmentVo> equipmentVoList = com.mt.roll.core.utils.BeanUtils.copyList(equipmentList, EquipmentVo.class);
        return successful(equipmentVoList);
    }
}
